package com.spc.car.web.log;

import com.spc.car.common.utils.MapExtendUtils;
import com.spc.car.common.utils.text.gson.GsonUtils;
import com.spc.car.web.log.annotation.LogAction;
import com.spc.car.web.log.annotation.LogProjectAction;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class BaseControllerInterceptor extends BaseInterceptor {

    public String getClassDesc(Class<?> clz) {
        String classDescription = "";

        LogAction classDesc = clz.getAnnotation(LogAction.class);

        if (null != classDesc) {
            classDescription = StringUtils.isBlank(classDesc.moduleName()) ? classDesc.value() : classDesc.moduleName();
        }

        if (StringUtils.isBlank(classDescription)) {
            classDescription = clz.getSimpleName();
        }

        return classDescription;
    }

    public String getMethodDesc(ProceedingJoinPoint joinPoint, Method currentMethod) {
        String methodDescription = "";

        LogAction methodDesc = currentMethod.getAnnotation(LogAction.class);

        if (null != methodDesc) {
            if (StringUtils.isBlank(methodDescription)) {
                methodDescription = StringUtils.isBlank(methodDesc.option()) ? methodDesc.value() : methodDesc.option();
            }
        }

        if (StringUtils.isBlank(methodDescription)) {
            methodDescription = currentMethod.getName();
        }

        return methodDescription;
    }

    @SuppressWarnings("unchecked")
    public Map<String, Object> getMethodParamsMap(ProceedingJoinPoint joinPoint, Method currentMethod) {
        Map<String, Object> params = new HashMap<>();

        if (canToJson(joinPoint.getArgs())) {
            params = getArgs(joinPoint, currentMethod);
            LogAction methodDesc = currentMethod.getAnnotation(LogAction.class);

            if(null != methodDesc) {
                // params 参数过滤.
                params = MapExtendUtils.filterMap(params, methodDesc.includeParams(), methodDesc.excludeParams());
                //log.info(GsonUtil.toJson(params, true));

                // HttpServletRequest 里面的参数过滤.
                String requestParamsKey = "HttpServletRequest";
                Map<String, Object> newRequestParameters = MapExtendUtils.filterMap((Map<String, Object>) params.get(requestParamsKey), methodDesc.includeParams(),  methodDesc.excludeParams());
                log.info("{}: ", requestParamsKey, GsonUtils.toJson(newRequestParameters, false));
            }

        } else {
            params.put("message","参数不能转换为json格式");
        }

        return params;
    }

    public Map<String, Object> getMethodParamsMap(ProceedingJoinPoint joinPoint,
                                                  Method currentMethod,
                                                  LogProjectAction projectAction) {
        Map<String, Object> params = new HashMap<>();

        if (canToJson(joinPoint.getArgs())) {
            params = getArgs(joinPoint, currentMethod);

            if(null != projectAction) {
                // params 参数过滤.
                params = MapExtendUtils.filterMap(params, null,
                        projectAction.excludeFlds());
                //log.info(GsonUtil.toJson(params, true));

                // HttpServletRequest 里面的参数过滤.
                String requestParamsKey = "HttpServletRequest";
                Map<String, Object> newRequestParameters = MapExtendUtils.filterMap((Map<String, Object>) params.get(requestParamsKey),
                        null,  projectAction.excludeFlds());
                log.info("{}: ", requestParamsKey, GsonUtils.toJson(newRequestParameters, false));
            }

        } else {
            params.put("message","参数不能转换为json格式");
        }

        return params;
    }

}
